package com.wing.device.controller.app;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.device.model.entity.Device;
import com.wing.device.model.request.DeviceForm;
import com.wing.device.model.response.DeviceVO;
import com.wing.device.service.DeviceService;
import com.wing.purchase.model.entity.SupplierInfo;
import com.wing.purchase.service.SupplierInfoService;
import com.wing.web.utils.LocaleMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Slf4j
@Validated
@Api(value = "App-设备表管理", tags = "设备表管理")
@RestController("appDeviceController")
@RequestMapping("device/app/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private LocaleMessage localeMessage;
    @Autowired
    private SupplierInfoService supplierInfoService;

    @ApiOperation(value = "查询所有设备表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
//            ,@ApiImplicitParam(name = "name", value="设备名称", defaultValue = "name", example = "name", dataType = "String", paramType = "query")
//            ,@ApiImplicitParam(name = "deviceCode", value="设备编码", defaultValue = "deviceCode", example = "deviceCode", dataType = "String", paramType = "query")

    })
    @GetMapping("/list")
    public PageResult<DeviceVO> list(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "1000") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "sort_number") String sort
            ,@RequestParam(name = "order",defaultValue = "asc") String order
            ,String orderCode
            ,String labelCode
            ,String deviceCode
            ,Long deviceCategoryId
            ,Long deviceBrandId
            ,Long deviceModelId
            ,Long deviceTypeId
            ,Long workshopId
    ) {

        Page<DeviceVO> pg = new Page<>(page, 1000);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("orderCode",orderCode);
        params.put("labelCode",labelCode);
        params.put("deviceCode",deviceCode);
        params.put("categoryId",deviceCategoryId);
        params.put("deviceBrandId",deviceBrandId);
        params.put("deviceModelId",deviceModelId);
        params.put("deviceType",deviceTypeId);
        if(workshopId!=null){
            params.put("workshopId",workshopId);
        }
        Page<DeviceVO> list = deviceService.pageList4app(pg, params);
//        if (list.getTotal()==0){
//            params.put("orderCode",null);
//            list = deviceService.pageList4app(pg, params);
//        }
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long deviceId) {

        DeviceVO deviceVO = deviceService.get4appById(deviceId);
        if (deviceVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), deviceVO);
    }

    @PostMapping("/addDevice")
    public JsonResult addDevice(@RequestBody DeviceForm deviceForm) {
        Device one = deviceService.getOne(new LambdaQueryWrapper<Device>().eq(Device::getDeviceCode, deviceForm.getDeviceCode()));
        if(one!=null){
            return JsonResult.fail("设备编号已存在，请重新输入！");
        }
        Device device = new Device();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(Device.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(deviceForm, device, copyOptions);
        if(device.getSupplierId()!=null){
            SupplierInfo supplierInfo = supplierInfoService.getById(device.getSupplierId());
            device.setSupplierName(supplierInfo.getCorporateName());
        }
        device.setStatus(Device.Status.normalUse);
        if (deviceService.save(device)) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

}
